package com.lab.dao.impl;

//数据访问层，学生信息CRUD操作

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.lab.po.PageBean;
import com.lab.po.Student;
import com.lab.util.MySqlSessionFactory;
import com.lab.dao.StudentDao;

public class StudentDaoImpl implements StudentDao {
	

	//获取指定的用戶(更新页面操作使用)
	public Student findStudentByid(int id) {
		SqlSession session=null;
		Student student=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			student=session.selectOne("StudentXml.findStudentByid", id);
			session.commit();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return student;
	}

	//添加学生信息
	public boolean addStudent(Student stu) {
		// Student(stId,stName,stSex,stAge,stTel,stDept,stAddress)
		SqlSession session=null;
		boolean flag = false;
		try {
			session=MySqlSessionFactory.getSqlSession();
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("stId", stu.getStId());
			map.put("stName", stu.getStName());
			map.put("stSex", stu.getStSex());
			map.put("stAge",stu.getStAge());
			map.put("stTel", stu.getStTel());
			map.put("maj_id", stu.getMaj_id());
			map.put("stAddress", stu.getStAddress());
			int i=session.insert("StudentXml.addStudent",map);
			session.commit();
			if (i == 1) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
	}

	//删除学生信息
	public boolean delStudent(int id) {
		boolean flag = false;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			int i=session.delete("StudentXml.delStudent", id);
			session.commit();
			if (i > 0) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
	}

	//更新学生信息
	public boolean updateStudent(Student stu) {
		boolean flag = false;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			Map<String,Object> map=new HashMap<String,Object>();
			
			map.put("stName", stu.getStName());
			map.put("stSex", stu.getStSex());
			map.put("stAge", stu.getStAge());
			map.put("stTel", stu.getStTel());
			map.put("maj_id", stu.getMaj_id());
			map.put("stAddress", stu.getStAddress());
			map.put("stId", stu.getStId());

			int i = session.update("StudentXml.updateStudent",map);
			session.commit();
			if (i == 1) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return flag;
	}

	//分页查询全体学生信息
	public List<Student> StSelect(int currentPage) {
		SqlSession session=null;
		List<Student> stuList=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			Map map=new HashMap();
			PageBean pb=new PageBean();
			
			map.put("key1", (currentPage-1)*pb.getCountPage());
			map.put("key2", pb.getCountPage());
			stuList=session.selectList("StudentXml.StSelect",map);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return stuList;
	}
	//根据id查找学生信息
	@Override
	public Student findStudentById(int id) {
		// TODO Auto-generated method stub
		Student student = null;
		SqlSession session=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			student=session.selectOne("StudentXml.findStudentById",id);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return student;
	}
	//生成学号
	@Override
	public int createstId(int maj_id) {
		SqlSession session=null;
		int stId=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			String stu_id = "____"+maj_id+"___";
			System.out.println(session.selectOne("StudentXml.createstId",stu_id));
			if(session.selectOne("StudentXml.createstId",stu_id)==null){
				stId=0;
			}else{
				stId=session.selectOne("StudentXml.createstId",stu_id);
			}
			
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return stId;
	}
	//查询学生总数
	@Override
	public int countStudent() {
		SqlSession session=null;
		int count=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			count=session.selectOne("StudentXml.countStudent");
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return count;
	}
	
	//模糊查询学生总数
	@Override
	public int countStudentByCondition(Map map) {
		// TODO Auto-generated method stub
		SqlSession session=null;
		int count=0;
		try {
			session=MySqlSessionFactory.getSqlSession();
			count=session.selectOne("StudentXml.countStudentByCondition",map);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return count;
	}

	//模糊查询学生(分页)
	@Override
	public List<Student> selectStudentByCon(Map map) {
		// TODO Auto-generated method stub
		SqlSession session=null;
		List<Student> stuList=null;
		try {
			session=MySqlSessionFactory.getSqlSession();
			stuList=session.selectList("StudentXml.selectStudentByCon",map);
			session.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(session!=null){
				session.close();
			}
		}
		return stuList;
	}

}
